$(function() {
    /* 实例化一个uploadToken的类 */
    new UploadToken({
        $el: $('[type=file]'),
        callback(url) {
            $('[type=file]').siblings('img')
                .attr('src', url)
                .css('zIndex', 1)

            $('[type=file]').siblings('.glyphicon-plus') && $('[type=file]').siblings('.glyphicon-plus')
                .css('zIndex', -1)
        }
    });

    $('#regForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            username: {
                message: '用户名无效',
                validators: {
                    notEmpty: {},
                    stringLength: {
                        min: 3,
                        max: 5
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/
                    },
                    different: {
                        field: 'password',
                        message: '用户名与密码不能重复'
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {},
                    stringLength: {
                        min: 3,
                        max: 5
                    },
                    identical: {
                        field: 'confirmPassword',
                        message: '两次输入的密码不相同'
                    },
                    different: {
                        field: 'username',
                        message: '密码不能与用户名重复'
                    }
                }
            },
            confirmPassword: {
                validators: {
                    notEmpty: {},
                    identical: {
                        field: 'password'
                    },
                    stringLength: {
                        min: 3,
                        max: 5
                    },
                    different: {
                        field: 'username',
                        message: '密码不能与用户名重复'
                    }
                }
            },
            pic: {
                validators: {
                    notEmpty: {
                        message: '必须上传图片'
                    },
                }
            }
        }
    }).on('success.form.bv', async function(e) {

        let nick_name = $('#username').val()
        let password = $('#password').val()
        let regStatus = await regUser({
            nick_name,
            password,
            avatar: $('img')[0].src
        }).catch(err => {
            alert(err)
            return;
        })
        $('[type="reset"]').trigger("click")
        localStorage.setItem('user_name', nick_name)
        localStorage.setItem('user_avatar', $('img')[0].src)
        if (regStatus) {
            location.replace('/')
        }
    })
})